#!/bin/bash

# Configuration

MENCODER_BIN="mencoder"
MENCODER_VIDEO_OPTIONS="-ovc xvid -xvidencopts pass=1"
MENCODER_AUDIO_OPTIONS="-channels 6 -oac lavc -lavcopts acodec=ac3:abitrate=384"
#MENCODER_DEBUG_OPTIONS="-ss 0 -endpos 20"

# Preparation

MENCODER_OPTIONS="$MENCODER_VIDEO_OPTIONS $MENCODER_AUDIO_OPTIONS"
MKV_FOLDER="$1"
MKV_EXT="mkv"
AVI_EXT="avi"

for MKV_FILE in $MKV_FOLDER/*.$MKV_EXT
do
	MKV_BASENAME=`basename $MKV_FILE .$MKV_EXT`
	MKV_FILENAME="$MKV_FOLDER/$MKV_BASENAME"
	SOURCE_FILENAME="$MKV_FILENAME.$MKV_EXT"
	DEST_FILENAME="$MKV_FILENAME.$AVI_EXT"
	echo "Converting from $SOURCE_FILENAME to $DEST_FILENAME..."
	echo "(Options: $MENCODER_DEBUG_OPTIONS $MENCODER_OPTIONS)"
	echo "========================================================="
	$MENCODER_BIN $SOURCE_FILENAME $MENCODER_DEBUG_OPTIONS $MENCODER_OPTIONS -o $DEST_FILENAME
done
